package com.art.museumbackend.controller;

import com.art.museumbackend.annotation.Log;
import com.art.museumbackend.entity.Result;
import com.art.museumbackend.entity.Role;
import com.art.museumbackend.service.RoleService;
import com.art.museumbackend.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/list")
    public Result<List<Role>> getRoleList() {
        return ResultUtil.success(roleService.list());
    }

    @PostMapping
    @Log(value = "添加角色")
    public Result<Void> addRole(@RequestBody Role role) {
        roleService.save(role);
        return ResultUtil.successMsg("添加角色成功");
    }

    @PutMapping
    @Log(value = "更新角色")
    public Result<Void> updateRole(@RequestBody Role role) {
        roleService.updateById(role);
        return ResultUtil.successMsg("更新角色成功");
    }

    @DeleteMapping("/{id}")
    @Log(value = "删除角色")
    public Result<Void> deleteRole(@PathVariable Integer id) {
        roleService.removeById(id);
        return ResultUtil.successMsg("删除角色成功");
    }

    @GetMapping("/permissions/{roleId}")
    public Result<List<Integer>> getRolePermissions(@PathVariable Integer roleId) {
        return ResultUtil.success(roleService.getPermissionsByRoleId(roleId));
    }

    @PostMapping("/permissions/{roleId}")
    @Log(value = "更新角色权限")
    public Result<Void> updateRolePermissions(@PathVariable Integer roleId, @RequestBody List<Integer> permissionIds) {
        roleService.updateRolePermissions(roleId, permissionIds);
        return ResultUtil.successMsg("更新角色权限成功");
    }
} 